/*
 * TestDumpCPPDirective.c
 *
 *  Created on: 2012/05/31
 *      Author: guanxiaoying
 */
#include "TestDump.h"

static VOID dumpDirective( AST astDir ){
	AST codescp = CDirectiveInfo.getCodeScp(astDir);
	U4 length;

	sprintf( tempBuf, header_format, dumpGetStr(XML_RECORD), astDir );
	dumpOutputStream( tempBuf );

	dumpCodeScopeAsField( codescp );

	if( CDirectiveInfo.isDefine( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#define" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_MACROINFO), "",
				CDirectiveInfo.define_getMacroInfo( astDir ) );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.define_getMacroName( astDir ) );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_MACROINFO)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isElif( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#elif" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_PPCONDEXP), "",
				CDirectiveInfo.elif_getCondtionExp( astDir ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_PPCONDEXP)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.elif_isActived( astDir )?"true":"false" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isElse( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#else" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.else_isActived( astDir )?"true":"false" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isEndif( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#endif" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isError( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#error" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.error_getMsg( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isIf( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#if" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_PPCONDEXP), "",
				CDirectiveInfo.if_getCondtionExp( astDir ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_PPCONDEXP)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.if_isActived( astDir )?"true":"false" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isIfdef( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#ifdef" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), ""  );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.ifdef_getLabel( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.ifdef_isActived( astDir )?"true":"false" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isIfndef( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#ifndef" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), ""  );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.ifndef_getLabel( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.ifndef_isActived( astDir )?"true":"false" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
		dumpOutputStream( tempBuf );
	}else if( CDirectiveInfo.isInclude( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#include" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );


		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), ""  );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.include_getHeader( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_FILE), "", CDirectiveInfo.include_getFileIndex( astDir ) );
		dumpOutputStream( tempBuf );
		length = CCodeScope.getFilePath(CDirectiveInfo.include_getFileIndex( astDir ), file_buf, OS_FULLPATH );
		if( length == 0 ){
			sprintf( file_buf, "%s", dumpGetStr( XML_ERROR ) );
		}
		dumpParameterStr(file_buf);
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_FILE)  );
		dumpOutputStream( tempBuf );
	}else if( CDirectiveInfo.isLine( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#line" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_VALUE), ""  );
		dumpOutputStream( tempBuf );
		sprintf( constantBuffer, "%ld", CDirectiveInfo.line_getLineNum( astDir )  );
		dumpParameterStr(constantBuffer );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_VALUE)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), ""  );
		dumpOutputStream( tempBuf );
		dumpParameterStr(CDirectiveInfo.line_getPath( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );

	}else if( CDirectiveInfo.isPragma( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#pragma" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), ""  );
		dumpOutputStream( tempBuf );
		dumpParameterStr(CDirectiveInfo.pragma_getCommand( astDir ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
		dumpOutputStream( tempBuf );
	}else if( CDirectiveInfo.isUndef( astDir ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "#undef" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_MACROINFO), "",
				CDirectiveInfo.undef_getMacroInfo( astDir ) );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CDirectiveInfo.undef_getMacroName ( astDir ) );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_MACROINFO)  );
		dumpOutputStream( tempBuf );
	}else{
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( dumpGetStr(XML_ERROR) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND), "" );
		dumpOutputStream( tempBuf );
	}

	sprintf( tempBuf, tail_format, dumpGetStr(XML_RECORD) );
	dumpOutputStream( tempBuf );

}
VOID DumpDirective( PSZ szPath, PSZ postfix ){
	AST astDir;
	AST astMax = CPPOutput.CDirectiveInfo->getMax();
	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		sprintf( tempBuf, top_header, dumpGetStr(XML_DIRECTIVE) );
		dumpOutputStream( tempBuf );
		for( astDir = MIN_ARRAY; astDir <= astMax; astDir++ ){
			dumpDirective( astDir );
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_DIRECTIVE) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}



